package 链表;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/partition-list/
 */
public class _86_分隔链表 {

    /**
     * 直接声明两个链表
     * 一个存小于target值的节点，一个存大于或等于target值的节点，
     * 再将两个节点拼接   （PS：不要忘记Large链表的尾部置为null，不然变成了循环链表！！！！）
     * @param head
     * @param x
     * @return
     */
    public ListNode partition(ListNode head, int x) {
        ListNode small = new ListNode(0);
        ListNode smallHead = small;
        ListNode large = new ListNode(0);
        ListNode largeHead = large;
        while (head != null) {
            if (head.val >= x) {
                large.next = head;
                large = large.next;
            } else {
                small.next = head;
                small = small.next;
            }
            head = head.next;
        }
        large.next = null;
        small.next = largeHead.next;
        return smallHead.next;
    }
}
